#include "stdafx.h"
#include "Dialogue.h"

Dialogue activeDialogue;

Dialogue::Dialogue() : bg(sf::Vector2f(2000.0f, 200.0f)), state(Finished), timer(.0f), skipFlag(false)
{
	bg.setFillColor(sf::Color(0,0,0,200));
	bg.setOrigin(1000.f, 0.0f);
	font.loadFromFile("data/font.ttf");
	sf::RenderStates rs;
	text.setFont(font);

	voiceBuffers[0].loadFromFile("data/sounds/m1.wav");
	voiceBuffers[1].loadFromFile("data/sounds/m2.wav");
	voiceBuffers[2].loadFromFile("data/sounds/f1.wav");
	voiceBuffers[3].loadFromFile("data/sounds/f2.wav");

	for (int i = 0; i < 4; ++i)
		voiceSounds[i].setBuffer(voiceBuffers[i]);
}

void Dialogue::Load(std::vector<const char*>& textList, std::vector<bool>& speakerList)
{
	state = Appearing;
	this->textList = &textList;
	this->speakerList = &speakerList;
	this->dialogueLength = textList.size();
	dialogueStep = 0;
	skipFlag = false;
	textShift = .0f;
	bg.setFillColor(sf::Color(0,0,0,0));

	if(textList.size() == 0)
		state = Finished;
	if(textList.size() != speakerList.size())
		std::cout << "ERROR: wrong list size!\n";
}

void Dialogue::Step()
{
	bool skipAnim = false;
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
	{
		if (!skipFlag)
		{
			skipFlag = true;
			skipAnim = true;
		}
	}
	else
		skipFlag = false;

	if (state == Appearing)
	{
		if (timer >= 1.0f || skipAnim)
		{
			bg.setFillColor(sf::Color(0,0,0,200));
			state = TextShowing;
			ResetText();
			timer = .0f;
		}
		else
		{
			bg.setFillColor(sf::Color(0,0,0, timer * 200.0f));
			timer += DELTA;
		}
	}
	else if (state == TextShowing)
	{
		if (timer >= 2.0f || skipAnim)
		{
			if (++dialogueStep == dialogueLength)
				state = Disappearing;
			else
				ResetText();
			timer = .0f;
		}
		else
		{
			float progress = timer * 0.8f;
			if (progress > 1.0f)
				progress = 1.0f;
			float scale = progress * progress;
			text.setScale(scale, scale);
			textShift = 120.f * (1.0f - sin(progress * PI_2));
			timer += DELTA;
		}
	}
	else if (state == Disappearing)
	{
		if (timer >= 1.0f || skipAnim)
		{
			state = Finished;
		}
		else
		{
			bg.setFillColor(sf::Color(0,0,0, (1.0f - timer) * 200.0f));
			activeColor.a = (1.0f - timer) * 255.0f;
			text.setColor(activeColor);
			timer += DELTA;
		}
	}
}

void Dialogue::Draw(sf::RenderWindow& window)
{
	if (state != Finished)
	{
		sf::View prev = window.getView();

		sf::Vector2u screenSize = window.getSize();
		if(screenSize.x == 0) screenSize.x = 0;
		sf::View view;
		view.setSize(sf::Vector2f(600 *  screenSize.x / screenSize.y, 600));
		view.setCenter(0, 0);
		window.setView(view);
		
		bg.setPosition(0, window.getView().getSize().y * 0.5f - bg.getSize().y);
		text.setPosition(0, 190.0f + textShift);
		window.draw(bg);
		window.draw(text);
	
		window.setView(prev);
	}
}

void Dialogue::ResetText()
{
	text.setString((*textList)[dialogueStep]);
	text.setOrigin(text.getLocalBounds().width * .5f, .0f);
	activeColor = (*speakerList)[dialogueStep] ? sf::Color(0,200,255,255) : sf::Color(255,0,200,255);
	text.setColor(activeColor);
	text.setScale(.0f, .0f);
	textShift = 120.f;
	int random = rand() % 2;
	if (activeSound)
		activeSound->stop();
	activeSound = &voiceSounds[random + ((*speakerList)[dialogueStep] ? 0 : 2)];
	activeSound->play();
}

bool Dialogue::IsActive()
{
	return state != Finished;
}